package cn.matrix42.potoo.format.markdown.element.chart.table;

import java.util.ArrayList;
import java.util.List;

import cn.matrix42.potoo.format.markdown.element.MarkdownElement;
import cn.matrix42.potoo.lang.Preconditions;
import cn.matrix42.potoo.lang.StringUtils;

public class TableRow extends MarkdownElement {

    private final int column;
    protected final List<String> rowContent;

    public TableRow(int column) {
        Preconditions.checkArgument(column > 0);
        this.column = column;
        this.rowContent = new ArrayList<>();
        for (int i = 0; i < column; i++) {
            rowContent.add("");
        }
    }

    public int getColumn() {
        return column;
    }

    public void set(int columnIndex, String content) {
        Preconditions.checkArgument(columnIndex < column);
        rowContent.set(columnIndex, content);
    }

    public void clear() {
        this.rowContent.clear();
    }

    @Override
    public List<String> toMarkdownStringList() {
        List<String> list = new ArrayList<>();
        list.add("|" + StringUtils.join(rowContent, "|") + "|");
        return list;
    }

}
